from google.appengine.ext import db

class Buyer(db.Model):
  gmtCreate = db.DateTimeProperty(auto_now_add=True)
  gmtModfied = db.DateTimeProperty(auto_now_add=True)
  id = db.IntegerProperty()
  category = db.StringProperty()
  mainProduct = db.StringProperty()
  country = db.StringProperty()
  area = db.StringProperty()
  companyName = db.StringProperty()
  address = db.StringProperty()
  postCode = db.StringProperty()
  tel = db.StringProperty()
  fax = db.StringProperty()
  mobile = db.StringProperty()
  contactName = db.StringProperty()
  email = db.EmailProperty()
  site = db.StringProperty()
  
  @staticmethod
  def getLastBuyer(limit, offset):
    query = db.Query(Buyer)
    query.order('-gmtCreate')
    return query.fetch(limit, offset)
  
  @staticmethod
  def getBuyerByCountry(country, limit, offset):
    query = db.Query(Buyer)
    query.filter('country = ', country)
    query.order('-gmtCreate')
    return query.fetch(limit, offset)

  @staticmethod
  def getBuyerCountByCountry(country):
    query = db.Query(Buyer)
    query.filter('country = ', country)
    return query.count()

  @staticmethod
  def getBuyerByCategory(category, limit, offset):
    query = db.Query(Buyer)
    query.filter('category = ', category)
    query.order('-gmtCreate')
    return query.fetch(limit, offset)

  @staticmethod
  def getBuyerCountByCategory(category):
    query = db.Query(Buyer)
    query.filter('category = ', category)
    return query.count()
  
  @staticmethod
  def isExistByCompanyName(companyName):
    query = db.Query(Buyer)
    query.filter('companyName = ', companyName)
    return not (query.count() <= 0)

